package com.process.Imp.team;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.process.controller.util.Code;
import com.process.controller.util.R;
import com.process.dao.team.*;
import com.process.domain.team.*;
import com.process.service.team.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @ClassName: TeamServiceImp
 * @Author: 殷凡卓
 * @Date: 2023/5/1 10:48
 */
@Service
public class TeamServiceImp extends ServiceImpl<TeamDao, Team> implements TeamService {
    @Autowired
    private TeamDao teamDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private TeamApplyDao teamApplyDao;
    @Autowired
    private MemberVoDao memberVoDao;
    @Autowired
    private TeamApplyVoDao teamApplyVoDao;


    @Override
    public Integer createTeam(long stuId) {
        MemberVo myDetil = memberVoDao.getMyDetil(stuId);
        if (myDetil == null) {
//       1.1设置队伍信息
            Date date = new Date();
            Team team = new Team();
            team.setCreationDate(date);
            team.setTeamState(0);
//       1.2新增队伍
            int Insert = teamDao.insert(team);
//       2.1添加队长Member
            long teamId = team.getTeamId();
            Member member = new Member();
            member.setStudentId(stuId);
            member.setTeamId(teamId);
            member.setMemberState(1);
//      2.2 添加队长信息
            memberDao.insert(member);
            return Code.SAVE_OK;

        } else {
//      3NO创建队伍
            return Code.SAVE_ERR;
        }
    }

    @Override
    public Integer deleteTeam(long stuId) {
        MemberVo myDetil = memberVoDao.getMyDetil(stuId);


//      1.查询是否是组员
//        Member member = memberDao.selectOne(new LambdaQueryWrapper<Member>().eq(Member::getStudentId, stuId));

//       2.1 是队长
        if (myDetil != null && myDetil.getMemberState() == 1) {
            teamDao.delete(new LambdaQueryWrapper<Team>().eq(Team::getTeamId, myDetil.getTeamId()));
            return Code.DELETE_OK;
        } else {
//      2.2 不是组员 || 不是队长
            return Code.DELETE_ERR;
        }
    }

    @Override
    public R applySend(long send, long accept) {
//      1.查询是否是队长
//        Member member = memberDao.selectOne(new LambdaQueryWrapper<Member>().eq(Member::getStudentId, send));
        MemberVo myDetil = memberVoDao.getMyDetil(send);

        if (myDetil == null) {
            return new R(Code.SAVE_ERR, null, "仅队长有权限发送邀请");
        } else if (myDetil.getMemberState() ==1){


            //      2.1队长填充信息
            Date date = new Date();
            TeamApply teamApply = new TeamApply();
            teamApply.setApplyDate(date);
            teamApply.setApplyTeamid(myDetil.getTeamId());
            teamApply.setSendStuid(send);
            teamApply.setGainStuid(accept);
            teamApply.setApplyState(0);
//      2.2新增
            int insert = teamApplyDao.insert(teamApply);

            return new R(Code.SAVE_OK, teamApply, "消息发送成功");

        }else
            return new R(Code.SAVE_ERR, null, "您没有权限");
    }


    @Override
    public List<TeamApplyVo> getSend(long stuId) {
//        Map<String, Object> query = new HashMap<String, Object>();
//        query.put("send_stuid", stuId);
//        List<TeamApply> teamApplies = teamApplyDao.selectByMap(query);

        List<TeamApplyVo> teamApplyVos = teamApplyVoDao.selectMySend(stuId);

        return teamApplyVos;
    }


    @Override
    public List<TeamApplyVo> getaccept(long stuId) {
//        Map<String, Object> query = new HashMap<String, Object>();
//        query.put("gain_stuid", stuId);
//        List<TeamApply> teamApplies = teamApplyDao.selectByMap(query);
        List<TeamApplyVo> teamApplyVos = teamApplyVoDao.selectMyAccept(stuId);

        return teamApplyVos;
    }

    @Override
    public List<MemberVo> getMyMember(long stuId) {
//       查询自己的队伍ID
        Member member = memberDao.selectOne(new LambdaQueryWrapper<Member>().eq(Member::getStudentId, stuId));

        if(member != null){
            long teamId = member.getTeamId();
            List<MemberVo> memberVos = memberVoDao.selectMyMembers(teamId);
            return memberVos;
        }
        else
            return null;


    }

    @Override
    public Integer acceptToTeam(long teamId, long stuId) {
        MemberVo myDetil = memberVoDao.getMyDetil(stuId);
//      数据填充
        Member member = new Member();
        member.setTeamId(teamId);
        member.setStudentId(stuId);
//      插入
        if (myDetil == null)
            return memberDao.insert(member);
        else
            return 0;
    }

    @Override
    public MemberVo getMyselfDetil(long stuId) {
        MemberVo myDetil = memberVoDao.getMyDetil(stuId);
        System.out.println(myDetil);

        return myDetil;
    }
}
